package vip.fuxk.blog.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.solon.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vip.fuxk.blog.dao.FuckScriptMapper;
import vip.fuxk.blog.entity.FuckScript;
import vip.fuxk.blog.service.FuckScriptService;

import java.util.List;


@Component
public class FuckScriptServiceImpl
        extends ServiceImpl<FuckScriptMapper, FuckScript>
        implements FuckScriptService {


    Logger log = LoggerFactory.getLogger(FuckScriptServiceImpl.class);


    @Cache(key = "fuck_script_query_by_path",tags = "fuck_script_query_by_path_${requestUrl}",seconds=5)
    public List<FuckScript> getByPathCache(String requestUrl){
        LambdaQueryWrapper<FuckScript> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FuckScript::getPath,requestUrl);
        queryWrapper.isNotNull(FuckScript::getPath);
        long c = this.count(queryWrapper);
        if(c!=1){
            if(c>1){
                log.warn("此url有多个脚本文件：{}",requestUrl);
            }else{
                log.warn("获取脚本失败,无此脚本:{}",requestUrl);
            }
            return CollUtil.newArrayList();
        }else{
            return this.list(queryWrapper);
        }
    }


}
